package moodish.test;

import java.io.Serializable;

import junit.framework.Assert;
import moodish.comm.ClientSideMessage;
import moodish.comm.ClientSideMessage.Type;
import moodish.dummy.comm.CSM;

import org.junit.Test;


public class CSMTestCase {

	private final static Type DEF_TYPE = Type.CONNECTED;
	private final static String DEF_SENDER_NICKNAME = "Sender Nickname";
	private final static String DEF_PAYLOAD = "Message Payload";
	
	@Test
	public void constructorTest1() {
		CSM clientSideMessage = new CSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
		Assert.assertNotNull(clientSideMessage);
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorTest2() {
		CSM clientSideMessage = new CSM(null, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
		Assert.assertNotNull(clientSideMessage);
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorTest3() {
		CSM clientSideMessage = new CSM(DEF_TYPE, null, DEF_PAYLOAD);
		Assert.assertNotNull(clientSideMessage);
	}

	@Test
	public void getTypeTest() {
		Type[] typesArray = Type.values();
		for (Type type : typesArray) {
			CSM clientSideMessage = new CSM(type, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertNotNull(clientSideMessage);
			Assert.assertEquals(type, clientSideMessage.getType());
		}
	}

	@Test
	public void getSendersNicknameTest() {
		CSM clientSideMessage = new CSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
		Assert.assertNotNull(clientSideMessage);
		Assert.assertEquals(DEF_SENDER_NICKNAME, clientSideMessage.getSendersNickname());
	}
	
	@Test
	public void getPayloadTest1() {
		CSM clientSideMessage = new CSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
		Assert.assertNotNull(clientSideMessage);
		Assert.assertEquals(DEF_PAYLOAD, clientSideMessage.getPayload());
	}
	
	@Test
	public void getPayloadTest2() {
		CSM clientSideMessage = new CSM(DEF_TYPE, DEF_SENDER_NICKNAME, null);
		Assert.assertNotNull(clientSideMessage);
		Assert.assertNull(clientSideMessage.getPayload());
	}
	
	@Test
	public void toStringTest1() {
		Type[] typesArray = Type.values();
		for (Type type : typesArray) {
			String expectedToString = "[" + type.toString() + "] - Sender:" + DEF_SENDER_NICKNAME + ", Payload: " + DEF_PAYLOAD + "."; 
			CSM clientSideMessage = new CSM(type, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
			Assert.assertNotNull(clientSideMessage);
			Assert.assertEquals(expectedToString, clientSideMessage.toString());
		}
	}
	
	@Test
	public void toStringTest2() {
		Type[] typesArray = Type.values();
		
		for (Type type : typesArray) {
			String expectedToString = "[" + type.toString() + "] - Sender:" + DEF_SENDER_NICKNAME + "."; 
			CSM clientSideMessage = new CSM(type, DEF_SENDER_NICKNAME, null);
			Assert.assertNotNull(clientSideMessage);
			Assert.assertEquals(expectedToString, clientSideMessage.toString());
		}
	}
	
	@Test
	public void isInstanceOfClientSideMessage() {
		CSM clientSideMessage = new CSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
		Assert.assertTrue(clientSideMessage instanceof ClientSideMessage);
	}
	
	@Test
	public void isInstanceOfSerializable() {
		CSM clientSideMessage = new CSM(DEF_TYPE, DEF_SENDER_NICKNAME, DEF_PAYLOAD);
		Assert.assertTrue(clientSideMessage instanceof Serializable);
	}
}
